package com.itheima.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.goods.pojo.Goods;
import com.itheima.goods.pojo.Spec;
import com.itheima.service.SpecService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {
    @Autowired
    private SpecService specService;

    @GetMapping("/findall")
    public Result findAll() {
        List<Spec> list = specService.list();
        if(list==null){
            return Result.builder().flag(false).code(StatusCode.ERROR).message("没有是数据").data(list).build();
        }
        return Result.builder().flag(true).code(StatusCode.OK).data(list).build();

    }

    @PostMapping("/add")
    public void add(@RequestBody Spec spec) {

        specService.save(spec);
    }

    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page") Integer page1, @PathVariable("size") Integer size) {
        IPage<Spec> page = new Page<>(page1,size);
       specService.page(page, null);
        PageResult<Spec> specPageResult = new PageResult<>();
       specPageResult.setRows(page.getRecords());
       specPageResult.setTotal(page.getTotal());
        if(page==null){
            return new Result(true,StatusCode.OK,"搜索失败");
        }
        return new Result(true,StatusCode.OK,"搜索成功",specPageResult);

    }



    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Spec spec = specService.getById(id);
        if(spec==null){
            return new Result(true,StatusCode.OK,"搜索失败");
        }
        return new Result(true,StatusCode.OK,"搜索成功",spec);
    }

    @PutMapping("/update/{id}")
    public Result update(@RequestBody Spec spec,@PathVariable("id") Integer id) {
        LambdaUpdateWrapper<Spec> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Spec::getId,id);
        boolean update = specService.update(spec, wrapper);
        if(!update){
            return new Result(true,StatusCode.OK,"修改失败");
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        boolean b = specService.removeById(id);
        if(!b){
            return new Result(true,StatusCode.OK,"删除失败");
        }
        return new Result(true,StatusCode.OK,"删除成功");

    }
}
